package com.yanqu.road.server.cmd.activity.zhenhuan.cross.thirdProcess;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogZhenHuanThirdJoinBanquet;
import com.yanqu.road.logic.activity.zhenhuan.ZhenHuanThirdProcessConfig;
import com.yanqu.road.logic.bussiness.DaoHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ZhenHuanProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.zhenhuan.ZhenHuanThirdProcessModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.zhenhuan.ZhenHuanConfigMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_ZHENHUAN_THIRD_PROCESS_JOIN_BANQUET_FROM_CROSS, desc = "甄嬛三加入宴会跨服回包")
public class ZhenHuanThirdProcessJoinBanquetFromCrossCmd extends AbstractCommand {
    private String reward;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ZhenHuanProto.CrossZhenHuanThirdProcessJoinBanquetRespMsg respMsg = ZhenHuanProto.CrossZhenHuanThirdProcessJoinBanquetRespMsg.parseFrom(packet.getBytes());
        ret = respMsg.getRet();
        reward = respMsg.getReward();

        ZhenHuanThirdProcessConfig config = ZhenHuanConfigMgr.getThirdProcessConfig();
        if (config == null) {
            ret = GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_CONFIG_NOT_FOUND;
            return;
        }
        if (player == null) {
            log.error("甄嬛加入宴会回包时,玩家不在内存了！！userId:{},reward:{}", packet.getPlayerId(), reward);
        } else {
            player.getModule(ZhenHuanThirdProcessModule.class).updateLastJoinTime(DateHelper.getCurrentTime());
            if (ret == 0) {
                // 获得奖励
                player.getModule(CurrencyModule.class).addCurrency(PropertyHelper.parseStringToProperty(reward), eLogMoneyType.ZhenHuanThirdProcess, eLogMoneyType.ZhenHuanThirdProcessJoinBanquetGet);
//                public LogZhenHuanThirdJoinBanquet(int activityId, long userId, int banquetId, int joinPos, String reward) {
                for (ZhenHuanProto.CrossZhenHuanThirdProcessJoinBanquetTemp joinBanquetTemp : respMsg.getJoinListList()) {
                    AutoLogMgr.add(new LogZhenHuanThirdJoinBanquet(config.getActivityId(), player.getUserId(), joinBanquetTemp.getBanquetId(), joinBanquetTemp.getPos(), joinBanquetTemp.getReward()));
                }
            }
        }

    }

    @Override
    public void sendData(GamePlayer player) {
        ZhenHuanProto.ZhenHuanThirdProcessJoinBanquetRespMsg.Builder respMsg = ZhenHuanProto.ZhenHuanThirdProcessJoinBanquetRespMsg.newBuilder();
        respMsg.setRet(ret);
        if (ret == 0) {
            respMsg.setReward(reward);
        }
        player.sendPacket(Protocol.U_ZHENHUAN_THIRD_PROCESS_JOIN_BANQUET, respMsg);
    }
}
